package com.sgj.gulimall.order.config;

import com.sgj.common.constant.RabbitConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目启动时创建队列和交换机并进行绑定
 * @Author: Guoji Shen
 * @Date: 2021/10/15 14:06
 */
@Slf4j
@Configuration
public class MyMQConfig {

    @Bean
    public Queue orderDelayQueue() {

        // 设置延时队列属性
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", RabbitConstant.ORDER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", RabbitConstant.ROUTING_KEY_ORDER_CREATE);
        arguments.put("x-message-ttl", 60000);

        // 创建延时队列
        return new Queue(RabbitConstant.ORDER_DELAY_QUEUE, true, false, false, arguments);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue(RabbitConstant.ORDER_RELEASE_QUEUE, true, false, false);
    }

    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(RabbitConstant.ORDER_EXCHANGE, true, false);
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding(RabbitConstant.ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.ORDER_EXCHANGE,
                RabbitConstant.ROUTING_KEY_ORDER_CREATE, null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(RabbitConstant.ORDER_RELEASE_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.ORDER_EXCHANGE,
                RabbitConstant.ROUTING_KEY_ORDER_RELEASE, null);
    }

    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding(RabbitConstant.STOCK_RELEASE_QUEUE, Binding.DestinationType.QUEUE, RabbitConstant.ORDER_EXCHANGE,
                RabbitConstant.ROUTING_KEY_ORDER_RELEASE_OTHER + ".#", null);
    }
}